import java.util.*;
import javax.swing.*;
import java.awt.*;
public class Example2 extends JFrame
{
	Container c = null;
	Graphics g = null;
	JPanel pn = new JPanel();
	public Example2()
	{
		c= getContentPane();
		c.add(pn,BorderLayout.CENTER);
		setSize(600,400);
		setVisible(true);
		g=pn.getGraphics();
	}
	public static void main(String args[])
	{
			Example2 app = new Example2();
			for (int i=0;i<=10;i++)
			{
				new Thread(new Drawer(i,20,i*30,app.pn)).start();
			}
	}

}
class Drawer implements Runnable
    {
		static Random n = new Random();
		int threadID,x,y;
		Graphics g;
		JPanel pn = null;
		public Drawer(int id,int x,int y,JPanel p)
		{
			threadID=id;
			this.x=x;
			this.y=y;
			pn=p;
			g=pn.getGraphics();
		}
		public void run()
		{
			int k=1;
			String time;
			while(true)
			{
				for(int i=0;i<threadID;i++)System.out.printf("    ");
				if(k==1)
				time = " time";
				else
				time = " times";
				g.setColor(Color.blue);
				g.drawString("I am thread "+threadID+"     ",x,y);
				
				try 
				{
					Thread.sleep(n.nextInt(2000-500+1)+500);
				}
				catch(InterruptedException e)
				{
				}
				g.setColor(pn.getBackground());
				g.fillRect(x,y-25,400,28);
				g.setColor(Color.blue);
				g.drawString("                                                 Drawer "+threadID+" has drawed "+k+time+". ",x,y);
				k++;
				pn.invalidate();
			}
		}
	}